using System;
using System.Collections.Generic;
using IFXplus.SmartStorage;

namespace IFXplus.Northwind
{
    public static class CustomerManager
    {
        public static List<Customer> GetCustomerList()
        {
            try
            {
                List<Customer> cl = CustomerListCache.Get();
                if (cl == null)
                {
                    cl = CustomerDataAccess.FindAll();
                    if (cl != null)
                        CustomerListCache.Insert(cl);
                }
                return cl;
            }
            catch(StorageException se)
            {
                throw new Exception("StorageException in CustomerManager.GetCustomerList", se);
            }
            catch(Exception ex)
            {
                throw new Exception("Exception in CustomerManager.GetCustomerList", ex);
            }
        }

        public static Customer GetCustomer(string customerid)
        {
            try
            {
                StorageItemMetadata meta = CustomerCache.GetMetadata(customerid);
                Customer customer = CustomerCache.Get(meta);
                if (customer == null)
                {
                    customer = CustomerDataAccess.Find(customerid);
                    if (customer != null)
                        CustomerCache.Insert(meta, customer);
                }
                return customer;
            }
            catch (StorageException se)
            {
                throw new Exception("StorageException in CustomerManager.GetCustomer", se);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in CustomerManager.GetCustomer", ex);
            }           
        }

        public static Customer CreateCustomer(string customerid, string companyname, string contactname, string contacttitle, string address, string city, string region, string postalcode, string country, string phone, string fax)
        {            
            Customer customer = new Customer();
            customer.CustomerId = customerid;
            customer.CompanyName = companyname;
            customer.ContactName = contactname;
            customer.ContactTitle = contacttitle;
            customer.Address = address;
            customer.City = city;
            customer.Region = region;
            customer.PostalCode = postalcode;
            customer.Country = country;
            customer.Phone = phone;
            customer.Fax = fax;
            CustomerDataAccess.Create(customer);

            StorageItemMetadata meta = CustomerCache.GetMetadata(customerid);
            CustomerCache.Insert(meta, customer);
            
            CustomerListCache.Remove();

            return customer;            
        }

        public static Customer UpdateCustomer(string customerid, string companyname, string contactname, string contacttitle, string address, string city, string region, string postalcode, string country, string phone, string fax)
        {
            Customer customer = new Customer();
            customer.CustomerId = customerid;
            customer.CompanyName = companyname;
            customer.ContactName = contactname;
            customer.ContactTitle = contacttitle;
            customer.Address = address;
            customer.City = city;
            customer.Region = region;
            customer.PostalCode = postalcode;
            customer.Country = country;
            customer.Phone = phone;
            customer.Fax = fax;
            CustomerDataAccess.Update(customer);

            StorageItemMetadata meta = CustomerCache.GetMetadata(customerid);
            CustomerCache.Insert(meta, customer);

            CustomerListCache.Remove();

            return customer;
        }

        public static void DeleteCustomer(string customerid)
        {
            CustomerDataAccess.Delete(customerid);

            StorageItemMetadata meta = CustomerCache.GetMetadata(customerid);
            CustomerCache.Remove(meta);

            CustomerListCache.Remove();
        }

    }
}